package org.dromara.patrol.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.patrol.domain.bo.PatrolActionQuery;
import org.dromara.patrol.domain.bo.PatrolActionStatisticsBo;
import org.dromara.patrol.domain.vo.*;
import org.dromara.patrol.service.IPatrolActionStatisticsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡店管理-巡店行为统计
 * 前端访问路由地址为:/patrol/actionStatistics
 *
 * @author han
 * @date 2024-04-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/actionStatistics")
public class PatrolActionStatisticsController extends BaseController {

    private final IPatrolActionStatisticsService patrolActionStatisticsService;

    /**
     * 查询巡店管理-巡店行为统计列表
     */
    @SaCheckPermission("patrol:actionStatistics:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolActionStatisticsVo> list(PatrolActionStatisticsBo bo, PageQuery pageQuery) {
        return patrolActionStatisticsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡店管理-巡店行为统计列表
     */
    @SaCheckPermission("patrol:actionStatistics:export")
    @Log(title = "巡店管理-巡店行为统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatrolActionStatisticsBo bo, HttpServletResponse response) {
        List<PatrolActionStatisticsVo> list = patrolActionStatisticsService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡店管理-巡店行为统计", PatrolActionStatisticsVo.class, response);
    }

    /**
     * 获取巡店管理-巡店行为统计详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("patrol:actionStatistics:query")
    @GetMapping("/{id}")
    public R<PatrolActionStatisticsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(patrolActionStatisticsService.queryById(id));
    }

    /**
     * 新增巡店管理-巡店行为统计
     */
    @SaCheckPermission("patrol:actionStatistics:add")
    @Log(title = "巡店管理-巡店行为统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolActionStatisticsBo bo) {
        return toAjax(patrolActionStatisticsService.insertByBo(bo));
    }

    /**
     * 修改巡店管理-巡店行为统计
     */
    @SaCheckPermission("patrol:actionStatistics:edit")
    @Log(title = "巡店管理-巡店行为统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolActionStatisticsBo bo) {
        return toAjax(patrolActionStatisticsService.updateByBo(bo));
    }

    /**
     * 删除巡店管理-巡店行为统计
     *
     * @param ids 主键串
     */
    @SaCheckPermission("patrol:actionStatistics:remove")
    @Log(title = "巡店管理-巡店行为统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(patrolActionStatisticsService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 任务状态-巡店行为统计
     * @param query
     * @return
     */
    @PostMapping("/status")
    public R<TaskStatusVo> status(@RequestBody PatrolActionQuery query) {
        return R.ok(patrolActionStatisticsService.selectTaskStatus(query));
    }

    /**
     * 门店覆盖率-巡店行为统计
     */
    @PostMapping("/storeIsCoveredCount")
    public R<StoreCoverVo> storeIsCoveredCount( @RequestBody PatrolActionQuery query) {
        return R.ok(patrolActionStatisticsService.selectStoreIsCoveredCount(query));
    }
    /**
     * 查询门店覆盖-巡店行为统计
     * @param query
     * @return
     */
    @PostMapping("/covered")
    public TableDataInfo<StoreCoverVo> storeCovered( @RequestBody PatrolActionQuery query) {
        return patrolActionStatisticsService.selectStoreCovered(query);
    }


    /**
     * 查询门店未覆盖-巡店行为统计
     * @param query
     * @return
     */
    @PostMapping("/notCovered")
    public TableDataInfo<StoreCoverVo>storeNotCovered(@RequestBody PatrolActionQuery query) {
        return patrolActionStatisticsService.selectStoreNotCovered(query);
    }


    /**
     * 查询按时完成率-巡店行为统计
     * @param query
     * @return
     */
    @PostMapping("/reportFinishOnTime")
    public R<PatrolFinishVo> reportFinishOnTime(@RequestBody PatrolActionQuery query) {
        return R.ok(patrolActionStatisticsService.selectReportFinishOnTime(query));
    }


    /**
     * 巡店情况统计-巡店行为统计
     * @param query
     * @return
     */
    @PostMapping("/patrolSituationCount")
    public R<PatrolSituationCountVo> patrolSituationCount(@RequestBody PatrolActionQuery query) {
        return R.ok(patrolActionStatisticsService.selectPatrolSituationCount(query));
    }
}
